package com.yunduansing.demo.config;

import javax.servlet.http.HttpServletResponse;

import com.yunduansing.demo.exception.MyException;
import com.yunduansing.demo.model.common.ResponseModel;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResponseModel globalException(HttpServletResponse response,Exception e){


        log.info("错误代码："  + response.getStatus()+" 错误信息："+e.getMessage());
        if(e instanceof MyException) {
            return ResponseModel.error(response.getStatus(),e.getMessage());
        }
        var result=ResponseModel.error(response.getStatus(),"error!");
        return result;
    }

}